var dataCount;
$(function () {
    $("#form-add").Validform({
        tiptype: 1,
        showAllError: true,
        datatype: {
            "empty": /^\s*$/,
            "code": /^[a-z0-9A-Z_-]{2,16}$/
        },
        callback: function (form) {
        	if($('#roleId').next().find('input').val()==''){
            	layer.msg('请选择角色')
            	return false;
            }
            layer.confirm('您确定要提交表单吗?', function () {
                _submit();
            })
            return false;
        }
    });
    $("#orgId").change(function () {
        var orgId = $.trim($("#orgId").val());
        $("#roleId").empty();
        $.ajax({
            type: "get",
            url: baseUrl + '/api/role/findRoleList?orgId=' + orgId,
            dataType: "json",
            success: function (result) {
                if (!result.flag) {
                    layer.msg(" 获取失败！原因：" + result.message);
                    return;
                }
                $.each(result.results, function (i, val) {
                    $("#roleId").append('<option value="' + val.id + '">' + val.name + '</option>');
                });
            }
        });
    });
    $("#orgId").change();
})

_submit = function () {
    var data = {};
    var user = {};
    var employee = {};
    user.loginName = $('#loginname').val();
    user.password = $('#userpassword').val();
    data.user = user;
    employee.empName = $('#empName').val();
    employee.code = $('#code').val();
    employee.marriageStatus = $('#marriageStatus').val();
    employee.sex = $('#sex').val();
    employee.homeAddress = $('#address').val();
    employee.mobilePhone = $('#phone').val();
    employee.email = $('#email').val();
    employee.officePhone = $('#officePhone').val();
    employee.professionalRanks = $('#professionalRanks').val();
    employee.idCardType = $('#idCardType').val();
    employee.idCardNo = $('#idCardNo').val();
    data.employee = employee;
    data.orgId = $('#orgId').val();
    data.roleId = $('#roleId').val();
    $.ajax({
        type: "post",
        url: baseUrl + '/api/employee/add',
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(data),
        success: function (result) {
            if (!result.flag) {
                layer.msg("增加失败！原因：" + result.message);
                return;
            }
            parent.location.reload();
            window.close();
        }
    });
}